package com.example.adminui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.adminui.entity.Team;
import com.example.adminui.entity.TeamMembers;
import com.example.adminui.entity.TeamPerformance;
import com.example.adminui.mapper.TeamMapper;
import com.example.adminui.mapper.TeamMembersMapper;
import com.example.adminui.mapper.TeamPerformanceMapper;
import com.example.adminui.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private TeamMembersMapper teamMembersMapper;

    @Autowired
    private TeamPerformanceMapper teamPerformanceMapper;

    @Override
    public List<Team> getAllTeams() {
        return teamMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public Team getTeamById(Long id) {
        return teamMapper.selectById(id);
    }

    @Override
    public void createTeam(Team team) {
        teamMapper.insert(team);
    }

    @Override
    public void updateTeam(Team team) {
        teamMapper.updateById(team);
    }

    @Override
    public void deleteTeam(Long id) {
        teamMapper.deleteById(id);
    }

    @Override
    public List<TeamPerformance> getTeamPerformanceByMonth(String month) {
        QueryWrapper<TeamPerformance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("month", month);
        return teamPerformanceMapper.selectList(queryWrapper);
    }

    @Override
    public TeamPerformance getTeamPerformanceById(Long id) {
        return teamPerformanceMapper.selectById(id);
    }

    @Override
    public void createTeamPerformance(TeamPerformance teamPerformance) {
        teamPerformanceMapper.insert(teamPerformance);
    }

    @Override
    public void updateTeamPerformance(TeamPerformance teamPerformance) {
        teamPerformanceMapper.updateById(teamPerformance);
    }

    @Override
    public void deleteTeamPerformance(Long id) {
        teamPerformanceMapper.deleteById(id);
    }

    @Override
    public void addTeamMember(Long teamId, Long userId) {
        TeamMembers teamMember = new TeamMembers();
        teamMember.setTeamId(teamId);
        teamMember.setUserId(userId);
        teamMember.setRole("member");
        teamMembersMapper.insert(teamMember);
    }

    @Override
    public void removeTeamMember(Long teamId, Long userId) {
        QueryWrapper<TeamMembers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_id", teamId).eq("user_id", userId);
        teamMembersMapper.delete(queryWrapper);
    }
}